package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    //添加检查项功能
    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }

    //分页查询功能
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        //获取页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取每页显示条数
        Integer pageSize = queryPageBean.getPageSize();
        //获取查询条件
        String queryString = queryPageBean.getQueryString();

        //分页插件进行分页
        PageHelper.startPage(currentPage,pageSize);
        //调用持久层的方法
        Page<CheckItem> page = checkItemMapper.findByPage(queryString);
        //返回集合
        List<CheckItem> list = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, list);
    }

    //根据id删除功能

    @Override
    public void deleteById(Integer id) {
        //先判断检查组中是否关联该检查项
        int length = checkItemMapper.find_Checkgroup_checkitem(id);
        if (length >0) {
            throw new RuntimeException("无法删除!");
        }else {
            checkItemMapper.deleteById(id);
        }
    }

    //根据id查询
    @Override
    public CheckItem findById(Integer id) {
        return checkItemMapper.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.findAll();
    }
}
